<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/css/style.css" type="text/css" media="all"/>
<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/artdialog/skins/default.css" type="text/css"
      media="all"/>
<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<link href="<?php echo URL::base() ?>media/vendor/fixtable/css/component.css" rel="stylesheet">
<link href="<?php echo URL::base() ?>media/vendor/fixtable/css/normalize.css" rel="stylesheet">
<link href="<?php echo URL::base() ?>media/vendor/css/jquery-ui-timepicker-addon.min.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-addon.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-zh-CN.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/Validform_v5.3.2_ncr_min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.printPage.js"></script>
<script src="<?php echo URL::base() ?>media/js/jquery.form.js"></script>
<style>
    .table td, .table th {
        text-align: center;
    }

    .form-horizontal .control-label {
        width: 100px;
    }

    .form-horizontal .controls {
        margin-left: 110px;
    }

    a.xiazai:hover {
        text-decoration: none;
    }

    table tbody tr:nth-child(2n) {
        background-color: #f5f5f5;
    }

    .table th, .table td {
        border: 1px solid #ffffff;
    }

    .gy-navtent2-input label {
        width: 120px;
        text-align: right;
    }

    .gy-navtent2-input {
        margin-bottom: 10px;
    }

</style>

<script type="text/javascript">
    $(document).ready(function () {
        $('#table_content > tr:odd').css("background-color", "#f5f5f5");
    });
</script>
<div class="gy-navtent2">
    <form class="form-inline" id="search_form">
        <div class="gy-navtent2-input">
            <label>库位：</label>
            <div class="input-append">
                <input class="span2" type="text" name="stor1"
                       value="<?php echo (isset($get['pre'])&&isset($get['stor1']))
                           ?$get['pre'].$get['stor1']:''; ?>"
                       onkeyup="checkStor(this)"
                       onafterpaste="checkStor(this)"
                       onblur="checkStor(this)"/>
                <span class="add-on">0000</span>
            </div>
            <label style="width: auto">到</label>

            <div class="input-append">
                <input class="span2" type="text" name="stor2"
                       value="<?php echo (isset($get['pre'])&&isset($get['stor2']))
                           ?$get['pre'].$get['stor2']:''; ?>"
                       onkeyup="checkStor(this)"
                       onafterpaste="checkStor(this)"
                       onblur="checkStor(this)"/>
                <span class="add-on">0000</span>
            </div>
            &nbsp;&nbsp;
            <label>库位搜索：</label>
            <input class="span2" type="text" placeholder="库位模糊搜索" name="stor"
                   value="<?php echo isset($get['stor'])?$get['stor']:''; ?>"
                   onkeyup="this.value = this.value.replace(/[^A-z0-9]/g,'');"/>
        </div>
        <div class="gy-navtent2-input">
            <label>SKU：</label>
            <input class="span2" type="text" name='sku' value="<?php echo isset($get['sku'])?$get['sku']:''; ?>"/>
            <label>截止日期(凌晨)：</label>
            <input type="text" name='end' class=" datepicker span2" id="end" readonly="readonly"
                   value="<?php echo date('Y-m-d', strtotime("+1 day")) ?>">
            <input type="submit" class="btn btn-info" value="查询"/>
            <div class="btn btn-success" id='download'><a class="xiazai" style="color:#FFF;">查询结果下载</a></div>
        </div>
    </form>
</div>
<div>
    <div>
        <div>
            <table class="table table1 table-bordered" style="border-collapse: collapse;">
                <thead>
                <tr style="background-color:#4EA392;color:#fff; font-weight:bold;">
                    <th>SKU</th>
                    <th>中文名称</th>
                    <th>储位</th>
                    <th>库存量</th>
                    <th>销售状态</th>
                </tr>
                </thead>
                <tbody id='table_content'>
                <?php
                if(isset($lists)){
                    foreach($lists as $list):?>
                        <tr>
                            <td><?php echo $list['sku'] ?></td>
                            <td><?php echo $list['name'] ?></td>
                            <td><?php echo $list['storage'] ?></td>
                            <td><?php echo $list['stock'] ?>  </td>
                            <td><?php echo $list['status']; ?></td>
                        </tr>
                    <?php endforeach;
                } ?>
                <tr>
                    <td colspan="13">
                        <div><?php if(isset($page)){
                                echo $page;
                            } ?></div>
                    </td>
                </tr>
                <tr>
                    <td colspan="13" style="text-align: center">
                        <div>总计<?php if(isset($num)){
                                echo $num;
                            } ?>个</div>
                    </td>
                </tr>
                </tbody>
            </table>
            <div id="pager_links"></div>
        </div>
        <div class="tab-pane" id="tab_2">
        </div>
    </div>
</div>

<script type="text/javascript">
    //储位筛选匹配
    function checkStor(s) {
        s = $(s);
        var c = s.val();
        s.val(c.replace(/[^A-z0-9]/g,''));
        if(c.length==4) {
            if(/^[A-z]{4}$/.test(c)) {
                alert('不能全部为字母');
                s.val(c.substr(0,3));
                return;
            }
            if(/^[A-z]{0,3}[0-9]{1,4}$/.test(c)) {

            } else {
                alert('不规则储位');
                s.val("");
            }
            return;
        }

        if(c.length>4 ) {
            alert('数字超过了4位');
            s.val(c.substr(0,4));
        }
    }

    function regStor(stor1, stor2) {
        var reg = /^[0-9]{1,4}$/;
        var reg2 = /^[A-z]{1,3}[0-9]{1,3}$/;
        if(stor1 || stor2) {
            if(stor1 && stor2) {
                if(reg.test(stor1) && reg.test(stor2)) {

                } else if(reg2.test(stor1) && reg2.test(stor2)) {
                    //判断包含字母是否相同
                    var pre1 = stor1.replace(/[^A-z]/g,'');
                    var pre2 = stor2.replace(/[^A-z]/g,'');
                    if(pre1 == pre2) {
                        return pre1;
                    } else {
                        alert('请输入相同字母开头的储位');
                        return false;
                    }
                } else {
                    alert('不允许的操作');
                    return false;
                }
            }
        }
        return true;
    }

    $("#search_form").submit(function(){
        var stor1 = $.trim($("input[name='stor1']").val());
        var stor2 = $.trim($("input[name='stor2']").val());

        var pre = regStor(stor1, stor2)

        if(pre) {
            if(typeof pre === "boolean") {
                pre = '';
            }

            var stor = $("input[name='stor']").val();
            var sku = $("input[name='sku']").val();
            var end = $("input[name='end']").val();
            var uri = 'sku=' + sku + '&end=' + end  + '&stor=' + stor;
            if(pre != '') {
                uri += '&pre=' + pre;
                stor1 = stor1.replace(pre, '');
                stor2 = stor2.replace(pre, '');
            }
            uri += '&stor1=' + stor1 + '&stor2=' + stor2;

            window.location.href = "<?php echo URL::base()?>storsearch/index?" + uri;
        }
        return false;
    });

    $("#download").click(function () {
        var stor1 = $.trim($("input[name='stor1']").val());
        var stor2 = $.trim($("input[name='stor2']").val());

        var pre = regStor(stor1, stor2)
        if(pre) {
            if(typeof pre === "boolean") {
                pre = '';
            }

            var stor = $("input[name='stor']").val();
            var sku = $("input[name='sku']").val();
            var end = $("input[name='end']").val();
            var uri = 'sku=' + sku + '&end=' + end  + '&stor=' + stor;
            if(pre != '') {
                uri += '&pre=' + pre;
                stor1 = stor1.replace(pre, '');
                stor2 = stor2.replace(pre, '');
            }
            uri += '&stor1=' + stor1 + '&stor2=' + stor2;

            window.location.href = "<?php echo URL::base()?>storsearch/index?download=1&" + uri;
        }
    });

    function clearNoNum(obj) {
        obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符

        obj.value = obj.value.replace(/^\./g, "");  //验证第一个字符是数字而不是.

        obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.

        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    }

    function reset_() {
        $("input[name='stor1']").val("");
        $("input[name='stor2']").val("");
        $("input[name='stor']").val("");
    }

    $(function () {
        $.datepicker.setDefaults({ dateFormat: 'yy-mm-dd' });
        $("#end").datepicker({
        });

        $("input[name='storage1']").keyup(function () {
            $("input[name='storage2']").val($(this).val());
        })
        $("input[name='storage1']").blur(function () {
            if ($(this).val().length < 4) {
                var len = 4 - $(this).val().length;
                var str = new Array(len + 1).join("0");
                $(this).val($(this).val() + str);
                $("input[name='storage2']").val($(this).val());
            }

            //添加搜索功能
            if ($(this).val() == '0000') {
                if ($("input[name='storage2']").val() == '0000') {
                    $("input[name='storage']").css({"color": ""});
                    $("input[name='storage']").attr("disabled", false);
                } else {
                    $("input[name='storage']").css({"color": "#888"});
                    $("input[name='storage']").attr("disabled", true);
                }
            } else {
                $("input[name='storage']").css({"color": "#888"});
                $("input[name='storage']").attr("disabled", true);
            }
        });
        $("input[name='storage2']").blur(function () {
            if ($(this).val().length < 4) {
                var len = 4 - $(this).val().length;
                var str = new Array(len + 1).join("0");
                $(this).val($(this).val() + str);
            }

            //添加搜索功能
            if ($(this).val() == '0000') {
                if ($("input[name='storage_position1']").val() == '0000') {
                    $("input[name='storage']").css({"color": ""});
                    $("input[name='storage']").attr("disabled", false);
                } else {
                    $("input[name='storage']").css({"color": "#888"});
                    $("input[name='storage']").attr("disabled", true);
                }
            } else {
                $("input[name='storage']").css({"color": "#888"});
                $("input[name='storage']").attr("disabled", true);
            }
        });
        //上传表格
        $(".upSuppForm").Validform({
            tiptype: 2,
            postonce: true
        });

        //添加模糊搜索
        $("input[name='storage']").blur(function () {
            if ($(this).val() == '') {
                $("input[name='stor1']").attr("disabled", false);
                $("input[name='storage2']").attr("disabled", false);
            } else {
                if ($(this).val() != '库位模糊搜索') {
                    $("input[name='storage1']").css({"color": "#888"});
                    $("input[name='storage2']").css({"color": "#888"});
                    $("input[name='storage1']").attr("disabled", true);
                    $("input[name='storage2']").attr("disabled", true);
                } else {
                    $("input[name='storage1']").attr("disabled", false);
                    $("input[name='storage2']").attr("disabled", false);
                }
            }
        });
    });
</script>